#define _CRT_SECURE_NO_WARNINGS 1
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
typedef struct ListNode ListNode;
struct ListNode* removeElements(struct ListNode* head, int val)
{
    ListNode* pcur = head;
    ListNode* newHead = NULL;
    ListNode* newTail = NULL;
    while (pcur)
    {
        if (pcur->val != val)
        {
            if (newHead == NULL)
            {
                newHead = newTail = pcur;
            }
            else
            {
                newTail->next = pcur;
                newTail = newTail->next;
            }
        }
        pcur = pcur->next;
    }
    if (newHead)
    {
        newTail->next = NULL;
    }
    return newHead;
}